﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using University.Workshop.Model;
using University.Workshop.Business;
using System.Threading;
using University.Workshop.DataAccess;
using System.Globalization;
using University.Workshop.DataAccess.LanguagePack.ClientLanguagePack;
namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Client client = new Client();
        bool userstate = false;
        bool passstate = false;
        bool retypestate = false;
        bool mailstate = false;
        Validate validate = new Validate();
       // BitmapImage bitmap;
        byte[] pict=new byte[1];
        int SizePic = 1;
        string selectedLanguage;
        string complete;
        string exists;
        string space;
        public MainWindow(string Language)
        {
            InitializeComponent();
            //client.receive();
            //client.Send("regular");
            //client.receive();
            question.SelectedIndex = 0;
            selectedLanguage = Language;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(this.selectedLanguage);
            ApplyLanguage();
            
        }

        private void ApplyLanguage()
        {
            complete = ClientLanguagePack.RegisterComplete;
            exists = ClientLanguagePack.RegisterExist;
            space = ClientLanguagePack.RegisterSpace;
            label1.Content = ClientLanguagePack.RegisterLabel1;
            label10.Content = ClientLanguagePack.RegisterLabel10;
            label11.Content = ClientLanguagePack.RegisterLabel11;
            label2.Content = ClientLanguagePack.RegisterLabel2;
            label3.Content = ClientLanguagePack.RegisterLabel3;
            label4.Content = ClientLanguagePack.RegisterLabel4;
            label5.Content = ClientLanguagePack.RegisterLabel5;
            label6.Content = ClientLanguagePack.RegisterLabel6;
            label7.Content = ClientLanguagePack.RegisterLabel7;
            label8.Content = ClientLanguagePack.RegisterLabel8;
            label9.Content = ClientLanguagePack.RegisterLabel9;
            
            label13.Content = ClientLanguagePack.RegisterLabel13;
            label14.Content = ClientLanguagePack.RegisterLabel14;
            label15.Content = ClientLanguagePack.RegisterLabel15;
            label16.Content = ClientLanguagePack.Registerlabel16;
            label17.Content = ClientLanguagePack.RegisterLabel17;
            label18.Content = ClientLanguagePack.RegisterLabel18;
            label19.Content = ClientLanguagePack.RegisterLabel19;
            label20.Content = ClientLanguagePack.RregisterLabel20;

            register.Content = ClientLanguagePack.RegisterButton1;
        
        }
        private void ValidateButton()
        {
            this.register.IsEnabled = userstate && passstate && retypestate && mailstate && answer.Text.Length > 0 && user.Text.Length > 0 && mail.Text.Length > 0;

        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            /*string path = image.browser();
            if (!String.IsNullOrEmpty(path))
            {
                pict = image.ConvertToByteString(path);
                bitmap = image.ConvertToBitMap(pict);
                if (bitmap != null)
                {
                    image1.Source = bitmap;
                    SizePic = pict.Length;
                }
            }*/
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (client.RegisterNewUser(user.Text, password.Password.GetHashCode().ToString(), mail.Text, question.SelectedIndex.ToString(), answer.Text.GetHashCode().ToString(), SizePic))
            {
                MessageBox.Show(complete);
                this.Close();
            }
            else
            {
                MessageBox.Show(exists);
               
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void user_TextChanged(object sender, TextChangedEventArgs e)
        {
            label10.Visibility = Visibility.Visible;
            userstate = false;

            if (!validate.ValidateUserName(user.Text,selectedLanguage) )
            {
                label10.Content = space;
                
            }
            else
            {
                label10.Content = validate.ValidateUserLength(user.Text);
              //  label10.Visibility = Visibility.Hidden;
                if(String.Compare(label10.Content.ToString()," ")==0)
                userstate = true;
            }
                ValidateButton();
        }

        private void mail_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!validate.ValidateEmail(mail.Text))
            {
                mailstate = false;
                label9.Visibility = Visibility.Visible;
            }
            else
            {
                mailstate = true;
                label9.Visibility = Visibility.Hidden;
            }
                ValidateButton();
        }

        private void password_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (!validate.ValidatePassword(password.Password))
            {
                label8.Visibility = Visibility.Visible;
                passstate = false;

            }
            else
            {
                label8.Visibility = Visibility.Hidden;
                passstate = true;
            }
            retypePass();
            ValidateButton();
        }
        private void retypePass()
        {
            if (retype.Password != password.Password)
            {
                label7.Visibility = Visibility.Visible;
                retypestate = false;
                }
            else
            {
                label7.Visibility = Visibility.Hidden;
                retypestate = true;
            }
         }
        private void retype_PasswordChanged(object sender, RoutedEventArgs e)
        {

            retypePass();            
                ValidateButton();
        }

        private void answer_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (answer.Text.Length == 0)
                label11.Visibility = Visibility.Visible;
            else
                label11.Visibility = Visibility.Hidden;
            ValidateButton();
        }

        private void button2_Click_1(object sender, RoutedEventArgs e)
        {
            ForgotPassword forgot = new ForgotPassword(selectedLanguage);
            forgot.Show();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
             
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {   /*User u = new User();
            u.Name = "nombre";
            u.Email = "asd@hotmail.com";
            u.Answer = "056456464";
            u.Password = "123123";
            u.Question = 0;

            EditProfile profile = new EditProfile(u);
         
            profile.Show();*/
            for (int i = 0; i < 500;i++ )

                client.RegisterNewUser(user.Text+i, password.Password.GetHashCode().ToString(), mail.Text, question.SelectedIndex.ToString(), answer.Text.GetHashCode().ToString(), SizePic);
            MessageBox.Show("ok");
        }

        
    }

}
